嗨嗨!今天就來讓我們來看一下這幾天寫的程式碼帶來的成果是甚麼吧!
讀者們可以看到這跟筆者先前教大家使用簡易的版本撰寫出來的程式的結果一樣!!
那今天筆者要再各位看幾個不一樣的範例!!
那這次就先一次全部將程式碼貼出來,提供給各位參考以及比較
這個是刪除資料的程式碼(DELETE)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
namespace DeleteSql
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ int a = 69;
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
conn.Open();
string sqlstr = "DELETE from table1 where [id] = @id";
SqlCommand cmd = new SqlCommand(sqlstr, conn);
cmd.Parameters.AddWithValue("id", a);
cmd.ExecuteNonQuery();
cmd.Cancel();
conn.Close();
Response.Write("have been delete");
}
}
}
這個是INSERT一筆資料的寫法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
namespace WebApplication22
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString)
{
int a = 69;
string b = "cxz";
int c = 890;
string sqlstr = "insert into table1 ([id], [name], [value]) values(@id, @name, @value)";
SqlCommand cmd = new SqlCommand(sqlstr, conn);
conn.Open();
cmd.Parameters.AddWithValue("id", a);
cmd.Parameters.AddWithValue("name", b);
cmd.Parameters.AddWithValue("value", c);
cmd.ExecuteNonQuery();
cmd.Cancel();
}
}
}
}
這個是UPDATE一筆資料的寫法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
namespace UpdateSql
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int a = 69;
string b = "newbrookie";
int c = 10;
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
string sqlstr = "UPDATE table1 SET [name] = @name ,[value]= @value WHERE [id] = @id";
SqlCommand cmd = new SqlCommand(sqlstr, conn);
conn.Open();
cmd.Parameters.AddWithValue("id", a);
cmd.Parameters.AddWithValue("name", b);
cmd.Parameters.AddWithValue("value", c);
cmd.ExecuteNonQuery();
cmd.Cancel();
conn.Close();
}
}
}